﻿using MaterialDesignColors;
using MaterialDesignThemes.Wpf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Media;
using UIWindows.Commands;
using UIWindows.Domains;
using UIWindows.Enums;
using UIWindows.GlobalStatics;
using UIWindows.Models.Interact;
using UIWindows.Tools.Helper;
using UIWindows.ViewModels.Dialogs;
using UIWindows.Views.Dialogs;
using XingLucifer.IBase.Enums;

namespace UIWindows.ViewModels
{
    public class SystemConfigViewModel : ViewModelBase
    {
        private bool _dataBool;

        public bool DataBool
        {
            get { return _dataBool; }
            set {
                if (value)
                {
                    DataListVisibility = System.Windows.Visibility.Visible;
                    ScanSignalVisibility = System.Windows.Visibility.Collapsed;
                }
                else
                {
                    DataListVisibility = System.Windows.Visibility.Collapsed;
                    ScanSignalVisibility = System.Windows.Visibility.Visible;
                }
                _dataBool = value;
            }
        }

        private System.Windows.Visibility _scanSignalVisibility;
        /// <summary>
        /// 扫描信号
        /// </summary>
        public System.Windows.Visibility ScanSignalVisibility
        {
            get { return _scanSignalVisibility; }
            set { SetProperty(ref _scanSignalVisibility, value); }
        }

        private System.Windows.Visibility _dataListVisibility;
        /// <summary>
        /// 交互集合
        /// </summary>
        public System.Windows.Visibility DataListVisibility
        {
            get { return _dataListVisibility; }
            set { SetProperty(ref _dataListVisibility, value); }
        }

        public object SystemParameter { get; set; }
        public object AddressStackPanel { get; set; }

        public SystemConfigStatic SCStatic { get; }
        private DeviceConfigStatic _deviceConfig;
        private UserConfigStatic _userConfig;
        public SystemConfigViewModel(SystemConfigStatic configStatic, DeviceConfigStatic deviceConfig, UserConfigStatic userConfig)
        {
            SCStatic = configStatic;
            StackPanel stack = new StackPanel();
            AddressStackPanel = stack;
            ControlsHelper.CreateChildren(stack, SCStatic.IAddress);
            StackPanel stack1 = new StackPanel();
            SystemParameter = stack1;
            ControlsHelper.CreateChildren(stack1, SCStatic.SCM);
            _deviceConfig = deviceConfig;
            _userConfig = userConfig;
        }

        public DelegateCommand ButSave => new DelegateCommand(() =>
        {
            SCStatic.Save();
            _ = $"系统配置修改，保存成功！\r\n操作人：[{_userConfig.LoginUser.Name}]".MessageDialogs(MessageLevelType.成功, true, PopUpTypes.Global);
            //$"用户：{_userConfig.LoginUser.Name} 修改系统配置".OperationLog(MessageLevelType.信息);
        }, AuthorityTypes.超级管理员_参照);

        #region 界面数据交互
        public DelegateCommand SignalAddClick => new DelegateCommand(() => DialogHost.Show(new NewScanSignal() { DataContext = new NewScanSignalViewDialog(SCStatic, default) }, PopUpTypes.Global.ToString()));
        public DelegateCommand InteractAddClick => new DelegateCommand(() => 
        DialogHost.Show(new NewInteractAddress() { DataContext = new NewInteractAddressViewDialog(SCStatic, _deviceConfig, default) }, PopUpTypes.Global.ToString()));

        public DelegateCommand PLCResectionAddClick => new DelegateCommand(() =>
        DialogHost.Show(new NewPLCResection() { DataContext = new NewPLCResectionViewDialog(SCStatic, default) }, PopUpTypes.Global.ToString()));
        public DelegateCommand<ScanSignalModel> SignalMouseDoubleClick => new DelegateCommand<ScanSignalModel>(sender =>
        {
            if (sender != null)
            {
                _ = DialogHost.Show(new NewScanSignal() { DataContext = new NewScanSignalViewDialog(SCStatic, sender, true) }, PopUpTypes.Global.ToString());
            }
        });
        public DelegateCommand<InteractAddressModel> InteractMouseDoubleClick => new DelegateCommand<InteractAddressModel>(sender =>
        {
            if (sender != null)
            {
                _ = DialogHost.Show(new NewInteractAddress() { DataContext = new NewInteractAddressViewDialog(SCStatic, _deviceConfig, sender, true) }, PopUpTypes.Global.ToString());
            }
        });

        public DelegateCommand<PLCResectionModel> PLCResectionMouseDoubleClick => new DelegateCommand<PLCResectionModel>(sender =>
        {
            if (sender != null)
            {
                _ = DialogHost.Show(new NewPLCResection() { DataContext = new NewPLCResectionViewDialog(SCStatic, sender, true) }, PopUpTypes.Global.ToString());
            }
        });
        //private Services.TaskService task = new Services.TaskService();
        public DelegateCommand Save => new DelegateCommand(() => {
            SCStatic.Save();
            "系统配置保存成功".MessageDialogs(MessageLevelType.成功, true, PopUpTypes.Global);
        });
        #endregion
    }
}
